﻿import QtQuick 2.0
import QtQuick.Controls 2.5


Item {
    id: root
    width: titleWidth + editWidth
    height: titleHeight
    property real radius: 3*dp
    property color color: Qt.rgba(245/255, 247/255, 250/255)


    property string title: ""       // 标题文字
    property int titleDirect: 0     // 标题文字位置 0左边 1上边
    property int titleWidth: title==="" ? 0 : (titleDirect===0 ? 80*dp : editWidth)
    property int titleHeight: title==="" ? 0 : (titleDirect===0 ? 40*dp : 30*dp)
    property var titleHAlign: Text.AlignRight       // 标题水平对齐方式
    property var titleVAlign: Text.AlignVCenter     // 标题垂直对齐方式
    property bool titleAlignJustify: true           // 标题是否两端对齐（设置为true时会覆盖titleHAline属性！！！）
    property color titleColor: "#000"

    property real editWidth: 180*dp
    property real editHeight: 40*dp

    property int maxValue: 999
    property int minValue: 0
    property int value: 0


    property int spinMode: 0        // 0左右排列 1靠右上下排列


    // 标题(选填)
    Item{
        id: rectTitle
        height: titleHeight
        width: titleWidth - 16*dp       // titleWidth比用户设置的titleWidth短一点
        Text{
            id: text_title
            text: {
                if(!titleAlignJustify) return title
                else{

                    if(text_rawTitle.contentWidth === 0 || text_space.contentWidth === 0){
                        return   title
                    }

                    return funcTools.justifyAlignAppendSpace(title, parent.width, text_rawTitle.width, text_space.width)
                }
            }
            font.pixelSize: config.fontsize
            anchors.fill: parent
            color: titleColor
            verticalAlignment: titleVAlign
            horizontalAlignment: titleAlignJustify ? Text.AlignHCenter : titleHAlign
        }
        Text{           // 固定在左边的星号
            visible: required
            text: '<font color="#FF0000"">* </font>'
            x: -10*dp
            font.pixelSize: config.fontsize
            height: titleHeight
            width: 10*dp
            verticalAlignment: Text.AlignVCenter
        }
    }
    Text{           // 用来计算空格的长度
        id: text_space
        text: " "
        font.pixelSize: config.fontsize
        visible: false
    }
    Text{           // 用来计算纯文本的长度
        id: text_rawTitle
        text: title
        font.pixelSize: config.fontsize
        visible: false
    }

    // 左边的钮
    CustomRectangle{
        height: editHeight; width: editWidth
        color: root.color
        radius_lt: root.radius
        radius_lb: root.radius
        Text{
            font.bold: true
            text: "-"
            opacity: 0.8
            anchors.centerIn: parent
            font.pixelSize: 28*dp
        }
        MouseArea{
            anchors.fill: parent
            cursorShape: Qt.PointingHandCursor
            onClicked: {
                textInput.text = parseInt(textInput.text) - 1
            }
        }
    }
    // 右边的钮
    CustomRectangle{
        height: root.height; width: root.height
        x: parent.width - width
        color: root.color
        radius_rt: root.radius
        radius_rb: root.radius
        Text{
            font.bold: true
            text: "+"
            opacity: 0.8
            anchors.centerIn: parent
            font.pixelSize: 25*dp
        }
        MouseArea{
            anchors.fill: parent
            cursorShape: Qt.PointingHandCursor
            onClicked: {
                textInput.text = parseInt(textInput.text) + 1
            }
        }
    }

    // 外框
    Rectangle{
        border.color: "#DDDDDD"
        color: "transparent"
        anchors.fill: parent
        radius: root.radius
    }
    // 内框
    Rectangle{
        border.color: "#DDDDDD"
        color: "transparent"
        height: parent.height
        width: root.width - root.height*2
        x: root.height
        TextInput{
            id: textInput
            text: value
            selectByMouse: true //设置为可以被鼠标选中
            anchors.centerIn: parent
            font.pixelSize: config.fontsize
            verticalAlignment: TextInput.AlignVCenter
            horizontalAlignment: TextInput.AlignHCenter
            anchors.fill: parent
            validator: RegExpValidator{
                regExp: /^[-+]?\d+/
            }

            onTextChanged: {
                if(text === "" || text === "-"){
                    value = 0
                    return
                }

                if(parseInt(text) > maxValue){
                    text = maxValue
                }else if(parseInt(text) < minValue){
                    text = minValue
                }
                value = parseInt(text)
            }
            onFocusChanged: {
                if(!focus && (text === "" || text === "-")){
                    text = 0
                    value = 0
                }
            }
        }
    }


}
